package com.lmxdawn.api.dnf.controller;

import com.lmxdawn.api.common.api.ApiErrorCode;
import com.lmxdawn.api.common.res.BaseResponse;
import com.lmxdawn.api.common.session.SessionContextHolder;
import com.lmxdawn.api.common.session.SessionHelper;
import com.lmxdawn.api.common.session.TokenHelper;
import com.lmxdawn.api.common.session.UserSession;
import com.lmxdawn.api.common.util.ResultVOUtils;
import com.lmxdawn.api.dnf.dto.RealnameReq;
import com.lmxdawn.api.dnf.entity.RealName;
import com.lmxdawn.api.dnf.service.IRealnameService;
import com.lmxdawn.api.dnf.dto.CommonReq;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangyubin
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/web/dnf/realname")
public class RealNameController {

    @Resource
    IRealnameService realnameService;

    /**
     * 获取列表
     * todo 移动端用户id获取
     */
    @PostMapping("/auth")
    public BaseResponse auth(@RequestBody CommonReq<RealnameReq> req, HttpServletRequest request) {

        boolean auth = realnameService.auth(req.getContent().getRealname(), req.getContent().getIdNo(), SessionContextHolder.getId());
        if (auth){
            String tokenValue = TokenHelper.getTokenValue(request);
            UserSession userSession = SessionHelper.getSessionStrategy().getUserSession(tokenValue);
            userSession.setRealName(auth);
            SessionHelper.getSessionStrategy().login(userSession, request, null);
            return ResultVOUtils.success(auth);
        } else {
            return ResultVOUtils.error(ApiErrorCode.REALNAME_FAIL.getCode(), ApiErrorCode.REALNAME_FAIL.getMsg());
        }

    }

    @PostMapping("/get")
    public BaseResponse get( HttpServletRequest request) {
        RealName one = realnameService.lambdaQuery().eq(RealName::getUserId,
                SessionContextHolder.getId()).eq(RealName::getStatus, 1).one();
//        if (one == null){
//            throw new NotRealnameException();
//        }
        return ResultVOUtils.success(one);

    }
}
